package com.itheima.controller;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Rest;
import com.itheima.pojo.Result;
import com.itheima.service.RestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
@RequestMapping("/rest")
@Validated
public class RestsController {
    @Autowired
    private RestService restService;

    @GetMapping
    public Result<PageBean<Rest>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String nickname,
            @RequestParam(required = false) String isadmin,
            @RequestParam(required = false) String search,
            @RequestParam(required = false) Integer userId
    ) {
        System.out.println(pageNum+"000");
        System.out.println(nickname+"111");
        System.out.println(isadmin+"222");
        System.out.println(userId+"333");
        PageBean<Rest> pb = restService.list(pageNum, pageSize, nickname, isadmin,search,userId);
        return Result.success(pb);
    }

    @PostMapping("add")
    public Result add(@RequestBody Rest rest) {
        System.out.println(rest.getCreateTime() + " 添加");
        restService.add(rest);
        return Result.success();
    }


    @PutMapping("/update")
    public Result update(@RequestBody @Validated Rest rest) {
        restService.update(rest);
        return Result.success();
    }

}
